package com.anbang.website.utils;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpClientUtil {
	protected static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
	
	public static String execute(HttpRequest req){
		String retString = "{code='9997',msg='后台服务器维护中...'}";
		CloseableHttpClient httpClient = HttpProtocolHandler.getInstance().getClient();
		String charset = req.getCharset();
		charset = charset == null ? Constant.CHARSET : charset;
		try{
			CloseableHttpResponse httpResponse = null;
			if(req.getMethod().equals(HttpRequest.METHOD_GET)){
				HttpGet method = new HttpGet(Constant.getServiceUrl().concat(req.getUrl()));
				String str = EntityUtils.toString(new UrlEncodedFormEntity(req.getNameValuePairs(), Consts.UTF_8));
				method.setURI(new URI(method.getURI().toString() + "?" + str));				
				httpResponse = httpClient.execute(method);
			}
			else if(req.getMethod().equals(HttpRequest.METHOD_POST)){
				HttpPost httpPost = new HttpPost(Constant.getServiceUrl().concat(req.getUrl()));
				httpPost.setEntity(new UrlEncodedFormEntity(req.getNameValuePairs(), Consts.UTF_8));
				httpResponse = httpClient.execute(httpPost);
			}
			try {
				HttpEntity entity = httpResponse.getEntity();
				String a = EntityUtils.toString(entity, Consts.UTF_8);
				if(httpResponse.getStatusLine().getStatusCode() == 200){
					retString = a;
				}
				else{
					retString ="{code='9996',msg='调用服务失败,请联系系统管理员.'}";
					logger.error(a);
				}				
				EntityUtils.consume(entity);
			} finally {
				httpResponse.close();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return retString;
	}
	
	public static String post(String url, String params){
		String retString = "{code='9997',msg='后台服务器维护中...'}";
		CloseableHttpClient httpclient = HttpProtocolHandler.getInstance().getClient();
		try {
			HttpPost httpPost = new HttpPost(url);
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("params", params));
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
			CloseableHttpResponse response = httpclient.execute(httpPost);

			try {
				HttpEntity entity = response.getEntity();
				String a = EntityUtils.toString(entity, Consts.UTF_8);
				if(response.getStatusLine().getStatusCode() == 200){
					retString = a;
				}
				else{
					retString ="{code='9996',msg='调用服务失败,请联系系统管理员.'}";
					logger.error(a);
				}				
				EntityUtils.consume(entity);
			} finally {
				response.close();
			}
		} catch (Exception e) {			
			e.printStackTrace();
		}
		return retString;
	}
	
	public static String get(String url, String params) {
		String retString = "{code='9997',msg='后台服务器维护中...'}";
		CloseableHttpClient httpclient = HttpProtocolHandler.getInstance().getClient();
		try {
			HttpGet httpget = new HttpGet(url);
			if(params !=null && !params.equals("")){
				List<NameValuePair> nvps = new ArrayList<NameValuePair>();
				nvps.add(new BasicNameValuePair("params", params));	
				String str = EntityUtils.toString(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
				httpget.setURI(new URI(httpget.getURI().toString() + "?" + str));
			}
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				HttpEntity entity = response.getEntity();
				String a = EntityUtils.toString(entity, Consts.UTF_8);
				if(response.getStatusLine().getStatusCode() == 200){
					retString = a;
				}
				else{
					retString ="{code='9996',msg='调用服务失败,请联系系统管理员.'}";
					logger.error(a);
				}				
				EntityUtils.consume(entity);
			} finally {
				response.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retString;
	}
}
